var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    cardnumber: '',
    phone: '',
    userpic: '',
    rhch: '',
    xhid: '',
    images: [],
    array_money: '',
    pictext: '上传身份证(正反两面)',
    array: ['会长', '副会长', '成员'],
    objectArray: [{
        id: '0',
        name: '会长'
      },
      {
        id: '1',
        name: '副会长'
      },
      {
        id: '2',
        name: '成员'
      },
    ],

  },
  name(e) {
    var that = this;
    that.setData({
      name: e.detail.value
    })
  },
  cardnumber(e) {
    var that = this;
    that.setData({
      cardnumber: e.detail.value
    })
  },
  phone(e) {
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  },
  bindPickerChange(e) {
    console.log(e)
    var that = this;
    for (var i = 0; i < that.data.objectArray.length; i++) {
      if (that.data.objectArray[i].id == e.detail.value) {
        that.setData({
          rhch: that.data.objectArray[i].name
        });
      }
    }
  },
  showok() {
    var that = this;
    if (that.data.name == '') {
      wx.showModal({
        title: '提示',
        content: '请填写姓名',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (that.data.cardnumber == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请填写身份证号码',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (that.data.phone == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请填写手机号码',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.request({
        url: app.serverUrl + 'Abouttwo/sqrh',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          openid: app.openid,
          name: that.data.name,
          position: that.data.rhch,
          img: that.data.images[0],
          img2: that.data.images[1],
          tel: that.data.phone,
          idcard: that.data.cardnumber,
          xhid: that.data.xhid
        },
        success: function(res) {
          wx.showModal({
            title: '提示',
            content: '申请入会提交成功，请等待人工审核！',
            success: function(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/ssxh/ssxh',
                })
              } else {
                console.log('用户点击取消')
              }
            }
          })

        },
      })
    }

  },
  removeImage(e) {
    console.log(e);
    var that = this;
    let images = that.data.images;
    const idx = e.currentTarget.dataset.idx;
    images.splice(idx, 1);
    that.setData({
      images: images
    })
  },

  handleImagePreview(e) {
    var that = this;
    const idx = e.currentTarget.dataset.idx;
    const images = that.data.images;
    wx.previewImage({
      current: images[idx], //当前预览的图片
      urls: images, //所有要预览的图片
    })
  },
  addImag(e) {
    console.log(e);
    var that = this;
    var thisid = e.target.id;
    // var images =[];
    wx.chooseImage({
      count: 2, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '正在上传...',
        })
        if (that.data.images.length == 2) {
          wx.showModal({
            title: '提示',
            content: '最多上传2张，请删除后在添加！',
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击取消')
              } else {
                console.log('用户点击取消')
              }
            }
          })
        }
        wx.uploadFile({
          url: app.serverUrl + 'About/articleUp', //仅为示例，非真实的接口地址
          articleimg: tempFilePaths[0],
          name: 'articleimg',
          filePath: tempFilePaths[0],
          openid: app.openid,
          success: function(res) {
            let images = that.data.images;
            images.push(res.data);
            images = images.length <= 2 ? images : images.slice(0, 2);
            that.setData({
              images: images,
            })
            wx.hideLoading();
          },
          fail: function(res) {
            // console.log(res);

          }
        })
        // }
      }
    })

  },
  chooseImage() {
    var that = this;
    wx.chooseImage({
      success: function(res) {
        wx.request({
          url: app.serverUrl + 'About/articleUp',
          // header: { 'content-type': 'application/x-www-form-urlencoded' },
          method: 'GET',
          dataType: 'json',
          data: {
            openid: app.openid,
            articleimg: res.tempFilePaths[0]
          },
          success: function(res) {
            that.setData({
              userpic: res.data.data.articleimg,
              pictext: '已上传'
            })
          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      xhid: options.xhid
    });
    wx.request({
      url: app.serverUrl + 'Abouttwo/costfind',
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        xhid: options.xhid
      },
      success: function(res) {
        console.log(res);
        that.setData({
          array_money: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})